home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16938 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: news.primenet.com!not-for-mail
  2. From: kj7bg@primenet.com (Bob White)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: HELP Borland C++ 5.0 pointer problem
  5. Date: 12 Apr 1996 13:17:00 -0700
  6. Organization: Primenet Services for the Internet
  7. Sender: root@primenet.com
  8. Message-ID: <4kmdns$7b7@nnrp1.news.primenet.com>
  9. References: <4klke8$7mb@dub-news-svc-2.compuserve.com>
  10. X-Posted-By: ip201.boi.primenet.com
  11. X-Newsreader: WinVN 0.99.7
  12. MIME-Version: 1.0
  13. Content-Type: Text/Plain; charset=US-ASCII
  14.  
  15. It worked in Win16, because you were LUCKY!  The "buffer" character array is 
  16. allocated on the local stack when the function is entered and deleted when the 
  17. function returns.  Hence, you are returning a pointer to a variable which has 
  18. been deleted.  You could solve this by getting a new area using "new" or 
  19. "malloc" and returning the ptr to it.  You would then have to delete or free 
  20. it when you were done.  Hope that helps.
  21.             
  22.             Bob
  23.  
  24.  
  25. In article <4klke8$7mb@dub-news-svc-2.compuserve.com>, 
  26. 100660.2242@compuserve.com says...
  27. >
  28. >Could you please tell me why the function below compiles and operates
  29. >correctly under win16 but not win32 ?
  30. >
  31. >  char* test_function(char *in_data) {
  32. >
  33. >        char buffer[20];
  34. >        strcpy(buffer, in_data);   // copy in_data to buffer
  35. >        in_data = buffer;   // set pointer to buffer
  36. >
  37. >        return(in_data);
  38. >
  39. >  }  // end test_function function
  40. >
  41. >Regards
  42. >
  43. >Andy Graham
  44. >
  45.  
  46.